﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;
using System.IO;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;

using KPIS.GERP.GFMS.APP.App_UI.Commons;
using KPIS.GERP.GFMS.DAL;

namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class FIM_PaymentBacks : System.Web.UI.Page
    {
        Main main = new Main();
        IBLL_FIM_Payments pay = new B_FIM_Payments();
        IBLL_BGM_MAS_Source source = new B_BGM_MAS_Source();
        IBLL_HRM_MP_WorkAreas area = new B_HRM_MP_WorkAreas();
        IBLL_FIM_Receives r = new B_FIM_Receives();
        IBLL_FIM_MAS_Contacts con = new B_FIM_MAS_Contacts();
        IBLL_FIM_MAS_Methods m = new B_FIM_MAS_Methods();
        IBLL_FIM_PaymentCheques pc = new B_FIM_PaymentCheques();
        IBLL_FIM_Petitions pet = new B_FIM_Petitions();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int year = main.GetSystemControls().default_bgm_year.Value;
                txtBgmYear.Text = year.ToString();
                txtBgmYear.MinValue = year - 10;
                txtBgmYear.MaxValue = year + 1;
                lblBgmYear.Text = year.ToString();

                UpdateUI1("ViewAll");
                LoadDDLFilterBgmSourceSeq();
                LoadDDLFilterWorkAreaSeq();
                LoadRBLMethodSeq();
                rblMethodSeq_SelectedIndexChanged(null, null);
                ddlFilterBgmSourceSeq.SelectedIndex = 0;
                lblBgmSourceName.Text = ddlFilterBgmSourceSeq.SelectedItem.Text;
                lblBgmYear.Text = txtBgmYear.Text;
                LoadDDLFilterPetitionSeq();
            }
        }

        protected void LoadRadGrid1()
        {
            string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            string strBgmYear = txtBgmYear.Text;
            string strWorkAreaSeq = ddlFilterWorkAreaSeq.SelectedValue;
            string strEventSeq = "23";
            RadGrid1.DataSource = pay.GetFull(strBgmSourceSeq, strBgmYear, strWorkAreaSeq, strEventSeq, null);
        }

        protected void LoadRadGrid2(string strPaySeq)
        {
            string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            string strBgmYear = txtBgmYear.Text;

            RadGrid2.DataSource = r.GetNotPaymentBacks(strBgmSourceSeq, null, strBgmYear, strPaySeq);
            RadGrid2.DataBind();
        }

        protected void CheckPayedReceives(string pay_seq)
        {
            for (int i = 0; i < RadGrid2.Items.Count; i++)
            {
                if (RadGrid2.Items[i]["pay_seq"].Text == pay_seq)
                {
                    RadGrid2.Items[i].Selected = true;
                }
            }
        }

        protected void LoadDDLFilterBgmSourceSeq()
        {
            ddlFilterBgmSourceSeq.DataSource = source.GetFull(true);
            ddlFilterBgmSourceSeq.DataBind();
        }

        protected void LoadDDLFilterWorkAreaSeq()
        {
            ddlFilterWorkAreaSeq.DataSource = area.GetList();
            ddlFilterWorkAreaSeq.DataBind();
        }

        protected void LoadDDLFilterPetitionSeq()
        {
            string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            string strBgmYear = txtBgmYear.Text;
            string strFilter = ddlFilterPetitionSeq.Text;

            ddlFilterPetitionSeq.DataSource = pet.GetList(strBgmSourceSeq, strBgmYear, strFilter);
            ddlFilterPetitionSeq.DataBind();
        }

        protected void LoadRBLMethodSeq()
        {
            rblMethodSeq.DataSource = m.GetList();
            rblMethodSeq.DataBind();

            rblMethodSeq.SelectedIndex = 0;
        }

        protected void UpdateUI1(string strCommand)
        {
            switch (strCommand)
            {
                case "ViewAll":
                case "Insert":
                case "Update":
                    cmdViewAll1.Visible = false;
                    cmdAddNew1.Visible = true;
                    cmdEdit1.Visible = true;
                    cmdDelete1.Visible = true;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = false;

                    PanelRadGrid1.Visible = true;
                    PanelEdit1.Visible = false;
                    break;
                case "AddNew":
                    cmdViewAll1.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = true;
                    cmdUpdate1.Visible = false;

                    PanelRadGrid1.Visible = false;
                    PanelEdit1.Visible = true;
                    break;
                case "Edit":
                    cmdViewAll1.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = true;

                    PanelRadGrid1.Visible = false;
                    PanelEdit1.Visible = true;
                    break;
            }
        }

        protected void ClearControl1()
        {
            lblPaySeq.Text = "";
        }

        //============================ control events ==============================

        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            //if (e.CommandName == "RowClick" && e.Item is GridDataItem)
            //{
            //    e.Item.Selected = true;
            //    string strReceiveSeq = e.Item.OwnerTableView.Items[e.Item.ItemIndex]["receive_seq"].Text;
            //    Session["ReceiveSeq"] = strReceiveSeq;
            //    LoadRadGrid2(strReceiveSeq);
            //}
        }

        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            if (!e.IsFromDetailTable)
            {
                LoadRadGrid1();
            }
        }

        protected void RadGrid2_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Sort" || e.CommandName == "Filter")
            {
                if (RadGrid1.SelectedItems.Count > 0)
                {
                    string strTransferSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["pay_seq"].Text;
                    LoadRadGrid2(main.WebString(strTransferSeq));
                }
                else
                {
                    LoadRadGrid2(null);
                }
            }
            else if (e.CommandName == "RowClick")
            {
                double dSum = 0;
                for (int i = 0; i < RadGrid2.Items.Count; i++)
                {
                    if (RadGrid2.Items[i].Selected)
                    {
                        dSum = dSum + Convert.ToDouble(main.WebString(RadGrid2.Items[i]["amount"].Text));
                    }
                }

                lblBathAmount.Text = dSum.ToString();
            }
        }

        protected void ddlFilterBgmSourceSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadRadGrid1();
            RadGrid1.DataBind();
            lblBgmSourceName.Text = ddlFilterBgmSourceSeq.SelectedItem.Text;
        }

        protected void ddlFilterWorkAreaSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadRadGrid1();
            RadGrid1.DataBind();
        }

        protected void txtBgmYear_TextChanged(object sender, EventArgs e)
        {
            LoadRadGrid1();
            RadGrid1.DataBind();
            lblBgmYear.Text = txtBgmYear.Text;
        }

        protected void cmdViewAll1_Click(object sender, EventArgs e)
        {
            UpdateUI1("ViewAll");
            ClearControl1();
        }

        protected void cmdAddNew1_Click(object sender, EventArgs e)
        {
            UpdateUI1("AddNew");

            LoadRadGrid2(null);
        }

        protected void cmdEdit1_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedItems.Count > 0)
            {
                UpdateUI1("Edit");

                string strPaySeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["pay_seq"].Text;
                //string strPayName = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["pay_name"].Text;
                string strRefNo = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["ref_no"].Text;
                string strPayDate = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["pay_date"].Text;
                string strRealPayDate = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["real_pay_date"].Text;
                string strMethodType = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["method_seq"].Text;
                string strAmount = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["amount"].Text;
                //string strContactSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["contact_seq"].Text;
                string strRemarks = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["remarks"].Text;

                LoadRadGrid2(strPaySeq);
                CheckPayedReceives(strPaySeq);

                lblPaySeq.Text = main.WebString(strPaySeq);
                //txtPayName.Text = main.WebString(strPayName);
                txtRefNo.Text = main.WebString(strRefNo);
                dpPayDate.SetValue(main.WebString(strPayDate));
                lblRealPayDate.Text = main.WebString(strRealPayDate);
                //ddlContactSeq.SelectedValue = main.WebString(strContactSeq);
                rblMethodSeq.SelectedValue = main.WebString(strMethodType);
                rblMethodSeq_SelectedIndexChanged(null, null);
                lblBathAmount.Text = strAmount;
            }
            else
            {
                main.MessageAlert(Page, "กรุณาเลือกรายการที่ต้องการแก้ไข");
            }
        }

        protected void cmdDelete1_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedItems.Count > 0)
            {
                M_FIM_Payments info = new M_FIM_Payments();
                string strUserID = Session["EmpSeq"].ToString();
                DateTime dtNow = DateTime.Now;
                string strPaySeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["pay_seq"].Text;

                info.pay_seq = Convert.ToInt32(strPaySeq);
                info.updated_by = main.ConvertNullInt(strUserID);
                info.updated_when = main.ConvertNullDateTime(dtNow);

                pay.Delete(info);

                LoadRadGrid1();
                RadGrid1.DataBind();
            }
        }

        protected void cmdInsert1_Click(object sender, EventArgs e)
        {
            M_FIM_Payments info = new M_FIM_Payments();
            
            string strPaySeq = lblPaySeq.Text;
            string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            string strWorkAreaSeq = ddlFilterWorkAreaSeq.SelectedValue;
            string strBgmYear = txtBgmYear.Text;
            string strEventSeq = "23";
            string strPayName = null;
            string strRefNo = txtRefNo.Text;
            string strPayDate = dpPayDate.GetStringValue();
            //string strContactSeq = ddlContactSeq.SelectedValue;
            string strAmount = lblBathAmount.Text;
            string strMethodSeq = rblMethodSeq.SelectedValue;
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;
            string strReceiveSeq;
            string strChequeSeq;
            string strChequeAmount;

            //info.transfer_seq = Convert.ToInt32(strTransferSeq);
            info.bgm_source_seq = Convert.ToInt32(strBgmSourceSeq);
            info.work_area_seq = Convert.ToInt32(strWorkAreaSeq);
            info.bgm_year = Convert.ToInt32(strBgmYear);
            info.event_seq = main.ConvertNullInt(strEventSeq);
            info.pay_name = strPayName;
            info.ref_no = strRefNo;
            info.pay_date = main.ConvertNullDateTime(strPayDate);
            info.contact_seq = null;
            info.amount = main.ConvertNullDouble(strAmount);
            info.method_seq = main.ConvertNullInt(strMethodSeq);

            info.created_by = main.ConvertNullInt(strUserID);
            info.created_when = main.ConvertNullDateTime(dtNow);

            if (strMethodSeq == "2")
            {
                if (info.amount < Convert.ToDouble(((Label)FIM_PaymentCheques1.FindControl("lblSumAmount")).Text))
                {
                    main.MessageAlert(Page, "จำนวนเงินในเช็ครวมทั้งหมด ห้ามเกินจำนวนเงินที่จ่าย!!");
                    return;
                }
            }

            strPaySeq = pay.Insert(info);

            for (int i = 0; i < RadGrid2.Items.Count; i++)
            {
                if (RadGrid2.Items[i].Selected)
                {
                    M_FIM_Receives receiveInfo = new M_FIM_Receives();
                    strReceiveSeq = RadGrid2.Items[i]["receive_seq"].Text;

                    receiveInfo.receive_seq = Convert.ToInt32(strReceiveSeq);
                    receiveInfo.pay_seq = main.ConvertNullInt(strPaySeq);
                    receiveInfo.updated_by = main.ConvertNullInt(strUserID);
                    receiveInfo.updated_when = dtNow;

                    r.UpdatePaySeq(receiveInfo);
                }
            }

            RadGrid RadGridPaymentCheques = (RadGrid)FIM_PaymentCheques1.FindControl("RadGrid1");

            for (int i = 0; i < RadGridPaymentCheques.Items.Count; i++)
            {
                if (RadGridPaymentCheques.Items[i].Selected)
                {
                    M_FIM_PaymentCheques PaymentChequeInfo = new M_FIM_PaymentCheques();
                    strChequeSeq = RadGridPaymentCheques.Items[i]["Cheque_seq"].Text;
                    strChequeAmount = ((RadNumericTextBox)RadGridPaymentCheques.Items[i].FindControl("txtAmount")).Text;

                    PaymentChequeInfo.cheque_seq = Convert.ToInt32(strChequeSeq);
                    PaymentChequeInfo.pay_seq = main.ConvertNullInt(strPaySeq);
                    PaymentChequeInfo.amount = main.ConvertNullDouble(strChequeAmount);
                    PaymentChequeInfo.updated_by = main.ConvertNullInt(strUserID);
                    PaymentChequeInfo.updated_when = dtNow;

                    pc.Insert(PaymentChequeInfo);
                }
            }

            LoadRadGrid1();
            RadGrid1.DataBind();
            UpdateUI1("Insert");
            ClearControl1();
        }

        protected void cmdUpdate1_Click(object sender, EventArgs e)
        {
            M_FIM_Payments info = new M_FIM_Payments();
            string strPaySeq = lblPaySeq.Text;
            string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            string strWorkAreaSeq = ddlFilterWorkAreaSeq.SelectedValue;
            string strBgmYear = txtBgmYear.Text;
            //string strEventSeq = rblEventSeq.SelectedValue;
            //string strPayName = txtPayName.Text;
            string strRefNo = txtRefNo.Text;
            string strPayDate = dpPayDate.GetStringValue();
            //string strContactSeq = ddlContactSeq.SelectedValue;
            string strAmount = lblBathAmount.Text;
            string strMethodSeq = rblMethodSeq.SelectedValue;
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;
            string strReceiveSeq;
            string strChequeSeq;
            string strChequeAmount;
            string strPayChequeSeq;

            info.pay_seq = Convert.ToInt32(strPaySeq);
            info.bgm_source_seq = Convert.ToInt32(strBgmSourceSeq);
            info.work_area_seq = Convert.ToInt32(strWorkAreaSeq);
            info.bgm_year = Convert.ToInt32(strBgmYear);
            //info.event_seq = main.ConvertNullInt(strEventSeq);
            //info.pay_name = strPayName;
            info.ref_no = strRefNo;
            info.pay_date = main.ConvertNullDateTime(strPayDate);
            info.contact_seq = null;
            info.amount = main.ConvertNullDouble(strAmount);
            info.method_seq = main.ConvertNullInt(strMethodSeq);

            info.updated_by = main.ConvertNullInt(strUserID);
            info.updated_when = main.ConvertNullDateTime(dtNow);

            if (strMethodSeq == "2")
            {
                if (info.amount < Convert.ToDouble(((Label)FIM_PaymentCheques1.FindControl("lblSumAmount")).Text))
                {
                    main.MessageAlert(Page, "จำนวนเงินในเช็ครวมทั้งหมด ห้ามเกินจำนวนเงินที่จ่าย!!");
                    return;
                }
            }

            pay.Update(info);

            for (int i = 0; i < RadGrid2.Items.Count; i++)
            {
                M_FIM_Receives receiveInfo = new M_FIM_Receives();
                strReceiveSeq = RadGrid2.Items[i]["receive_seq"].Text;

                receiveInfo.receive_seq = Convert.ToInt32(strReceiveSeq);
                receiveInfo.updated_by = main.ConvertNullInt(strUserID);
                receiveInfo.updated_when = main.ConvertNullDateTime(dtNow);

                if (RadGrid2.Items[i].Selected)
                {
                    receiveInfo.pay_seq = Convert.ToInt32(strPaySeq);

                    r.UpdateDepositSeq(receiveInfo);
                }
                else
                {
                    receiveInfo.pay_seq = null;

                    r.UpdateDepositSeq(receiveInfo);
                }
            }

            RadGrid RadGridPaymentCheques = (RadGrid)FIM_PaymentCheques1.FindControl("RadGrid1");

            for (int i = 0; i < RadGridPaymentCheques.Items.Count; i++)
            {
                if (main.WebString(RadGridPaymentCheques.Items[i]["pay_cheque_seq"].Text).Length == 0)
                {
                    if (RadGridPaymentCheques.Items[i].Selected)
                    {
                        M_FIM_PaymentCheques PaymentChequeInfo = new M_FIM_PaymentCheques();
                        strChequeSeq = main.WebString(RadGridPaymentCheques.Items[i]["cheque_seq"].Text);
                        strChequeAmount = ((RadNumericTextBox)RadGridPaymentCheques.Items[i].FindControl("txtAmount")).Text;

                        PaymentChequeInfo.cheque_seq = Convert.ToInt32(strChequeSeq);
                        PaymentChequeInfo.pay_seq = main.ConvertNullInt(strPaySeq);
                        PaymentChequeInfo.amount = main.ConvertNullDouble(strChequeAmount);
                        PaymentChequeInfo.updated_by = main.ConvertNullInt(strUserID);
                        PaymentChequeInfo.updated_when = dtNow;

                        pc.Insert(PaymentChequeInfo);
                    }
                }
                else
                {
                    M_FIM_PaymentCheques PaymentChequeInfo = new M_FIM_PaymentCheques();
                    strPayChequeSeq = main.WebString(RadGridPaymentCheques.Items[i]["pay_cheque_seq"].Text);

                    PaymentChequeInfo.pay_cheque_seq = Convert.ToInt32(strPayChequeSeq);
                    PaymentChequeInfo.updated_by = main.ConvertNullInt(strUserID);
                    PaymentChequeInfo.updated_when = dtNow;

                    if (RadGridPaymentCheques.Items[i].Selected)
                    {
                        strPaySeq = main.WebString(RadGridPaymentCheques.Items[i]["pay_seq"].Text);
                        strChequeSeq = main.WebString(RadGridPaymentCheques.Items[i]["cheque_seq"].Text);
                        strChequeAmount = ((RadNumericTextBox)RadGridPaymentCheques.Items[i].FindControl("txtAmount")).Text;

                        PaymentChequeInfo.cheque_seq = Convert.ToInt32(strChequeSeq);
                        PaymentChequeInfo.pay_seq = main.ConvertNullInt(strPaySeq);
                        PaymentChequeInfo.amount = main.ConvertNullDouble(strChequeAmount);
                        
                        pc.Update(PaymentChequeInfo);
                    }
                    else
                    {
                        pc.Delete(PaymentChequeInfo);
                    }
                }
            }

            LoadRadGrid1();
            RadGrid1.DataBind();
            UpdateUI1("Update");
            ClearControl1();
        }

        //protected void ddlContactSeq_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        //{
        //    string strFilter = e.Text;
        //    IList<M_FIM_MAS_Contacts> infolist = con.GetList(strFilter);
        //    int itemOffset = e.NumberOfItems;
        //    int endOffset = Math.Min(itemOffset + 10, infolist.Count);
        //    e.EndOfItems = endOffset == infolist.Count;

        //    for (int i = itemOffset; i < endOffset; i++)
        //    {
        //        ddlContactSeq.Items.Add(new RadComboBoxItem(infolist[i].contact_name, infolist[i].contact_seq.ToString()));
        //    }

        //    e.Message = main.GetStatusMessage(endOffset, infolist.Count);
        //}

        protected void rblMethodSeq_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rblMethodSeq.SelectedValue == "2")
            {
                FIM_PaymentCheques1.Visible = true;
                //((Label)FIM_PaymentCheques1.FindControl("lblPaySeq")).Text = lblPaySeq.Text;
                FIM_PaymentCheques1.PaymentCheques_Set(lblPaySeq.Text, ddlFilterBgmSourceSeq.SelectedValue, 250);
            }
            else
            {
                FIM_PaymentCheques1.Visible = false;
            }
        }

        protected void ddlFilterPetitionSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadRadGrid1();
            RadGrid1.DataBind();
        }

        //==========================================================================
    }
}
